package com.yh.service.impl;

import com.yh.mapper.RoleMapper;
import com.yh.pojo.Permission;
import com.yh.pojo.Role;
import com.yh.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author by 张晨光
 * @date 2023/11/7 15:28
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Override
    public List<Role> findAllRoles() {
        return roleMapper.findAllRoles();
    }

    @Override
    public void addRole(Role role) {
        roleMapper.addRole(role);
    }

    @Override
    public Role findById(Integer id) {
        return roleMapper.findById(id);
    }

    @Override
    public List<Permission> findOtherPermission(Integer id) {
        return roleMapper.findOtherPermission(id);
    }

    @Override
    public void addPermissionToRole(Integer roleId, Integer[] permissions) {
        for (Integer permissionId : permissions) {
            roleMapper.addPermissionToRole(roleId,permissionId);
        }
    }

    @Transactional
    @Override
    public void deleteRoleByRoleId(Integer id) {
        //1.删除
        roleMapper.deleteUsersRoleByRoleId(id);
        //2.删除
        roleMapper.deleteRolePermissionByRoleId(id);
        //3.删除
        roleMapper.deleteRoleByRoleId(id);
    }
}
